package LUNCH;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.*;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class LunchInfoXml {
	String filePath = "";

	public LunchInfoXml(String filePath) {
		this.filePath = filePath;
	}

	public static void main(String[] args) {
		try {
			long begin = System.currentTimeMillis();
			LunchInfoXml x = new LunchInfoXml("src/LUNCH/Lunch.xml");
			x.readXML();
			long end = System.currentTimeMillis();
			System.out.println("java dom用时" + (end - begin) + "毫秒");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void prt(Map<String, List<String[]>> map) {
		Iterator it1 = null;
		Map.Entry entry = null;
		it1 = map.entrySet().iterator();
		while (it1.hasNext()) {
			entry = (Map.Entry) it1.next();
			System.out.println("姓名: " + entry.getKey());
			List<String[]> list = (List<String[]>) entry.getValue();
			int len = list.size();
			for (int i = 0; i < len; i++) {
				String arr[] = list.get(i);
				for (String value : arr) {
					System.out.println(value);
				}
				System.out.println("********************************************");
			}
		}
	}

	public Map<String, List<String[]>> readXML() throws ParserConfigurationException, SAXException,
			IOException {

		Map<String, List<String[]>> map = new LinkedHashMap<String, List<String[]>>();

		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();// 1.获取工厂
		DocumentBuilder builder = factory.newDocumentBuilder();// 2.产生解析器
		Document document = builder.parse(new File(filePath));// 3.解析xml文档，得到代表文档的document
		NodeList list = document.getElementsByTagName("姓名");// 4.获取所有姓名节点
		List<String[]> InfoList = null;
		for (int i = 0; i < list.getLength(); i++) {// 5.遍历所有所有姓名节点
			Node node = list.item(i);
			Element currentElement = null;
			if (node.getNodeType() == Node.ELEMENT_NODE) {// 6.在做节点转换之前先判断，这样安全。
				currentElement = (Element) node;// 7.当前姓名节点
				String currentAttributeName = currentElement
						.getAttribute("name");// 8.当前姓名节点的属性名
				if (currentAttributeName != null
						&& currentAttributeName.length() > 0) {// 姓名属性不为空
					currentAttributeName = currentAttributeName.replaceAll(
							"\\s*", "");
					if (currentAttributeName.length() == 0) {
						continue;
					}
					NodeList list1 = node.getChildNodes();// 遍历当前节点的子节点
					String LunchInfo[] = new String[4];
					LunchInfo[0] = currentAttributeName;
					int count = 1;
					for (int i1 = 0; i1 < list1.getLength(); i1++) {
						Node child = list1.item(i1);
						if (getAllNode(child).length() > 0) {
							LunchInfo[count] = getAllNode(child);
							count++;
						}
					}
					InfoList = map.get(currentAttributeName);
					if (InfoList != null) {
						InfoList.add(LunchInfo);
					} else {
						InfoList = new ArrayList<String[]>();
						InfoList.add(LunchInfo);
						map.put(currentAttributeName, InfoList);
					}
				}
			}
		}
		//prt(map);
		return map;
	}

	public String getAllNode(Node node) {
		String content = "";
		Element currentElement = null;
		if (node.getNodeType() == Node.ELEMENT_NODE) {
			String currentNodeContent = node.getTextContent();
			String currentNodeName = node.getNodeName();
			currentNodeContent = currentNodeContent.replaceAll("\\s*", "");
			currentNodeName = currentNodeName.replaceAll("\\s*", "");
			if (currentNodeContent.length() > 0) {
				content = currentNodeContent;
			}
		}
		return content;
	}
}
